/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hcmus.fit.master.sevensea.sprite.ship;

import hcmus.fit.master.sevensea.background.Sea;
import hcmus.fit.master.sevensea.core.Constant;
import hcmus.fit.master.sevensea.util.Queue;

import java.util.List;
import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.Point;

/**
 * 
 * @author hvp
 */
public class RedPirate extends Ship {
    public static List<List<Bitmap>> Resource;
    public static int NumOfStatus;

    public RedPirate(Sea seamap, int x, int y) {
        super(seamap, x, y);
        setFrames(Resource.get(Status));
        setNbOfFrame(getFrames().size());
        FrameLibrary = Resource;
    }

    public RedPirate(RedPirate pirate) {
        super(pirate);
    }

    public int GetScore() {
        return 60;
    }

    public void GetDirection(int targetX, int targetY) {
        int[] cld = { -1, -1, 0, 1, 1, 1, 0, -1 };
        int[] clc = { 0, 1, 1, 1, 0, -1, -1, -1 };
        boolean[][] dd = new boolean[Constant.Height + 2][];
        Point[][] pre = new Point[Constant.Height + 2][];
        for (int i = 0; i < Constant.Height + 2; i++) {
            dd[i] = new boolean[Constant.Width + 2];
            pre[i] = new Point[Constant.Width + 2];
            dd[i][0] = dd[i][Constant.Width + 1] = true;
        }
        for (int i = 1; i < Constant.Height + 1; i++) {
            for (int j = 1; j < Constant.Width + 1; j++) {
                if ((getSeaMap().map[i - 1][j - 1] != null)
                        && (!getSeaMap().map[i - 1][j - 1].isShip()))
                    dd[i][j] = true;
            }
        }
        for (int i = 1; i < Constant.Width + 1; i++) {
            dd[Constant.Height + 1][i] = dd[0][i] = true;
        }

        Point current = new Point(X + 1, Y + 1);
        Point u = current;
        Queue<Point> queue = new Queue<Point>();
        queue.Enqueue(u);

        while (queue.Count() > 0) {
            u = (Point) queue.Dequeue();
            Random r = new Random();
            int random = r.nextInt(2);
            boolean repeat = true;
            int i = random * 7;

            while (repeat) {
                Point v = new Point(u.x + clc[i], u.y + cld[i]);

                if (!dd[v.y][v.x]) {
                    dd[v.y][v.x] = true;
                    pre[v.y][v.x] = u;
                    if (v.x == (targetX + 1) && v.y == (targetY + 1)) {
                        // dung
                        Point k = v;
                        while (!pre[k.y][k.x].equals(current)) {
                            k = pre[k.y][k.x];
                        }
                        int deltaX = k.x - X - 1;
                        int deltaY = k.y - Y - 1;

                        if (deltaX < 0 && deltaY < 0) {
                            DestDirection = 7;
                        }
                        // trai
                        else if (deltaX < 0 && deltaY == 0) {
                            DestDirection = 6;
                        }
                        // trai duoi
                        else if (deltaX < 0 && deltaY > 0) {
                            DestDirection = 5;
                        }
                        // tren
                        else if (deltaX == 0 && deltaY < 0) {
                            DestDirection = 0;
                        }
                        // duoi
                        else if (deltaX == 0 && deltaY > 0) {
                            DestDirection = 4;
                        }
                        // phai tren
                        else if (deltaX > 0 && deltaY < 0) {
                            DestDirection = 1;
                        }
                        // phai
                        else if (deltaX > 0 && deltaY == 0) {
                            DestDirection = 2;
                        }
                        // phai duoi
                        else if (deltaX > 0 && deltaY > 0) {
                            DestDirection = 3;
                        }
                        return;
                    } else {
                        queue.Enqueue(v);
                    }
                }
                if (random == 0) {
                    i++;
                    if (i >= 8) {
                        repeat = false;
                    }
                } else {
                    i--;
                    if (i < 0) {
                        repeat = false;
                    }
                }
            }
        }
    }

    public boolean isEnemy() {
        return true;
    }
}
